$(document).ready(function(){
	var type=$('#type').val();
	var hosId=$('#hosId').val();
	//初始化form表单
	initForm(type,hosId);
	
	//事件绑定
	eventBind(type,hosId);
});

//初始化form表单
function initForm(type,hosId){
	$('#hospitalForm').validation();
	
	if(type=='add'){
		genDictSelect('hosLevelInfo','hospital_level','1');
		genDictSelect('hosRankInfo','hospital_rank','1');
		initOwnerProvinceSel(null,null,null);
//		initParentSel(null,null);
	}else{
		$.getApiClient('/baseInfo/hospital/{hospitalid}').setQuery({hospitalid: hosId}).get().done(function(data){
			if(data.code=='0'){
				var payload=data.payload;
				$('#hospitalForm').bindData(payload);
				genDictSelect('hosLevelInfo','hospital_level',payload.hospital_level);
				genDictSelect('hosRankInfo','hospital_rank',payload.hospital_rank);
				initOwnerProvinceSel(payload.owner_province,payload.owner_city,payload.owner_country);
//				initParentSel(payload.parent_id,hosId);
				$('#hospital_code').attr('readonly','readonly');
			}
		})
	}
	
	function initOwnerProvinceSel(dfPprovinceVal,dfCityVal,dfCountryVal){
		$.getApiClient('/locationarea/provices').setQuery({}).get().done(function(data){
			if(!data.payload){
				data.payload=[];
			}
		    multipleSelectData("owner_province",[{"code": "", "name": "请选择"}].concat(data.payload), dfPprovinceVal, {valueAttr:'code', displayAttr:'name'},'请选择');
			$('#owner_province').multipleSelect({width:'100%', single: true});
			
			$('#owner_province').change(function(){
				var provinceCode=$(this).children('option:selected').val();
				initOwnerCitySel(provinceCode,dfCityVal,dfCountryVal);
			}).change();
		});
	}
	
	function initOwnerCitySel(provinceCode,dfCityVal,dfCountryVal){
		$.getApiClient('/locationarea/citys').setQuery({provinceCode:provinceCode}).get().done(function(data){
			if(!data.payload){
				data.payload=[];
			}
		    multipleSelectData("owner_city",[{"code": "", "name": "请选择"}].concat(data.payload), dfCityVal, {valueAttr:'code', displayAttr:'name'},'请选择');
			$('#owner_city').multipleSelect({width:'100%', single: true});
			
			$('#owner_city').change(function(){
				var city=$(this).children('option:selected').val();
				initOwnerCountrySel(city,dfCountryVal);
			}).change();
		});
	}
	
	function initOwnerCountrySel(cityCode,dfCountryVal){
		$.getApiClient('/locationarea/countys').setQuery({cityCode:cityCode}).get().done(function(data){
			if(!data.payload){
				data.payload=[];
			}
		    multipleSelectData("owner_country",[{"code": "", "name": "请选择"}].concat(data.payload), dfCountryVal, {valueAttr:'code', displayAttr:'name'},'请选择');
			$('#owner_country').multipleSelect({width:'100%', single: true});
		});
	}
	
	/**
	 * //初始化上级医院级拉框
	function initParentSel(defaultVal,hosId){
		$.getApiClient('/baseInfo/hospitals').setQuery({}).get().done(function(data){
			if(!data.payload){
				data.payload=[];
			}
			if(hosId){
				//过滤掉自身
				data.payload=$.grep(data.payload,function(item,index){
					return item['id'] != hosId;
				})
			}
		    multipleSelectData("parent_id",[{"id": "", "hospital_name": "请选择"}].concat(data.payload), defaultVal, {valueAttr:'id', displayAttr:'hospital_name'});
			$('#parent_id').multipleSelect({width:'100%', single: true});
		});
	}
	**/
}

//事件绑定
function eventBind(type,hosId){
	$('#cancel').click(function(){
		BootstrapDialog.closeAll();
	})
	
	if(type=='add'){
		$('#save').on('click',saveNewHospital);
	}else{
		$('#save').on('click',function(){
			updateHospital(hosId);
		});
	}
}

//新增医院操作
function saveNewHospital(){
	if(!$("#hospitalForm").validationForm()){
		return;
	}
	
	$.getApiClient('/baseInfo/hospital').post('#hospitalForm', function(params){
		return $.extend(params, {
//			parent_name: $('#parent_id').multipleSelect('getSelects','text')[0]
		});
	}).done(function(data){
		if(data.code == "0"){
			BootstrapDialog.closeAll();
			customTimeOutDialog('信息提示','保存成功',null,2000);
			pageQuery();
		}
	});
}

//修改医院操作
function updateHospital(hosId){
	if(!$("#hospitalForm").validationForm()){
		return;
	}
	
	$.getApiClient('/baseInfo/hospital/{hospitalid}').setQuery({
		hospitalid:hosId
	}).put('#hospitalForm', function(params){
		return $.extend(params, {
//			parent_name: $('#parent_id').multipleSelect('getSelects','text')[0]
		});
	}).done(function(data){
		if(data.code == "0"){
			BootstrapDialog.closeAll();
			customTimeOutDialog('信息提示','修改成功',null,2000);
			pageQuery();
		}
	});
}